package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.model.article.mess.UpdateArticleMess;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
@Slf4j
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper,ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    @Override
        public ResponseResult like(LikesBehaviorDto dto) {
            // 1. 检查参数(已经在实体类中定了校验注解)

            ApUser user = AppThreadLocalUtils.getUser();
            // 2. 查询行为实体数据
            ApBehaviorEntry apBehaviorEntry = this.apBehaviorEntryService.findByUserIdOrEquipmentId(user == null ? null : user.getId(), dto.getEquipmentId());
            if (apBehaviorEntry ==null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            // 3. 根据操作类型 进行对应的操作
            if (dto.getOperation().intValue() == 0) {
                //点赞
                ApLikesBehavior apLikesBehavior = this.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                        .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                        .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));

                if (apLikesBehavior != null){
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在,请勿重复点赞");
                }
                apLikesBehavior = new ApLikesBehavior();
                apLikesBehavior.setEntryId(apBehaviorEntry.getId());
                apLikesBehavior.setArticleId(dto.getArticleId());
                apLikesBehavior.setType((short)0);
                apLikesBehavior.setOperation(dto.getOperation());
                apLikesBehavior.setCreatedTime(new Date());
                this.save(apLikesBehavior);
            }else {
                //取消点赞
                this.remove(Wrappers.<ApLikesBehavior>lambdaQuery()
                        .eq(ApLikesBehavior::getArticleId,dto.getArticleId())
                        .eq(ApLikesBehavior::getEntryId,apBehaviorEntry.getId()));
            }

            //*****添加文章点赞-发送消息******
            // =======================新加代码==========================
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            mess.setArticleId(dto.getArticleId());
            mess.setAdd(dto.getOperation().intValue()==0?1:-1);
            kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(mess));
            log.info("点赞行为 发送kafka消息 ==>{}",JSON.toJSONString(mess));
            // =======================新加代码==========================
            return ResponseResult.okResult();
    }
}
